<?php

namespace common\models;
use Yii;
use yii\data\Pagination;
/**
 * This is the model class for table "knowledge".
 *
 * @property integer $id
 * @property string $knowID
 * @property string $Title
 * @property string $knowcontent
 * @property string $description
 * @property string $Contribute
 * @property string $knowclassifyID
 * @property string $keywords
 * @property string $language
 * @property string $addtime
 */
class KnowledgeModel extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public $subject;
    public $grade;
    
    public static function tableName()
    {
        return 'knowledge';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['addtime','knowid', 'title', 'knowcontent', 'description', 'contribute', 'keywords', 'language','classifygrade','classifysubject'], 'safe'],
            [['knowid', 'title', 'knowcontent', 'description', 'contribute', 'keywords', 'language'], 'string', 'max' => 255],
            ['classifygrade','string','min'=>2],//保证选择了学科年级
            ['classifysubject','string','min'=>2]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'knowid' => '知识点标识',
            'title' => '知识点名称',
            'knowcontent' => '知识点内容',
            'description' => '知识点描述',
            'contribute' => '添加人',
            'keywords' => '关键字',
            'language' => '语言',
            'addtime' => '添加时间',
            'subject'=>'学科',
            'grade'=>'年级',
            'classifygrade'=>'年级',
            'classifysubject'=>'学科',
        ];
    }
    
    //通过id来获取知识点及其分类
    public function getknowbyid($id)
    {
        $data= self::find()->where('id=:id',[':id'=>$id])->one();
        //产生的是的对象，通过idname来获取其中的值
//        $data->classifygrade=KnowClasGradeModel::find()->where('knowClassObjectID=:classifygrade',[':classifygrade'=>$data->classifygrade])->one()
//                ->idname;
//        $data->classifysubject= KnowClasSubjectModel::find()->where('knowClasSubejctID=:classifysubject',[':classifysubject'=>$data->classifysubject])->one()
//                ->idname;
        return $data;
    }

    public function editknow($data,$id)
    {
        if($this->load($data)){
            //使用块赋值
//            if($data['KnowledgeModel']['classifygrade']==0 || $data['KnowledgeModel']['classifysubject']==0){
//                \Yii::$app->session->setFlash('chooseClasify','请选择学科年级');
//                return false;
//            }
           $this->attributes=$data['KnowledgeModel'];
//           $this->addtime= date();
           return (bool)$this->updateAll([
               'knowid'=> $this->knowid,
               'title'=> $this->title,
               'knowcontent'=> $this->knowcontent,
               'description'=> $this->description,
               'contribute'=> $this->contribute,
               'keywords'=> $this->keywords,
               'language'=> $this->language,
//               'addtime'=> $this->addtime,
               'classifygrade'=> $this->classifygrade,
               'classifysubject'=> $this->classifysubject
                ],'id=:id',[':id'=> $id]);
        } 
        return false;
           
    }
    
    //添加知识点
    public function addknowway($data)
    {
        if($this->load($data) &&$this->validate()){
             $this->attributes=$data['KnowledgeModel'];
             $this->addtime= date('Y-m-d H:i:s', time());
             if($this->save()){
                 return TRUE;
             }
             \Yii::$app->session->setFlash('addknow','请选择学科年级');
             return false;
        }
        \Yii::$app->session->setFlash('addknow','添加失败，请重新添加');
        return false;
    }

        public function getSearch($data,$pageSize)
    {
        //返回两个值$pager,$data
//        $mysubject= $data['KnowledgeModel']['subject'];
//        $content="数学";
        $mygrade= $data['KnowledgeModel']['grade'];
//        $sql="select *
//from knowledge
//WHERE
//knowledge.description LIKE  '%{$content}%' AND
//knowledge.grade =  '{$grade}' AND
//knowledge.subjects =  '数学'
//";
//        $result=self::findBySql($sql);
        
        $obj= self::find()->where('classifygrade = :classifygrade',[':classifygrade'=>$mygrade]);
        $count=$obj->count();
        $pager=new Pagination(['totalCount'=>$count,'pageSize'=>$pageSize]);
        $result=$obj->offset($pager->offset)->limit($pager->limit)->all();
//        var_dump($mygrade);var_dump($result);exit;
        $myData['data']=$result;
        $myData['pager']=$pager;
        return $myData;
       //数据查找
//        $sql="SELECT
//            knowclassify.knowID
//            FROM
//            knowclassify
//            WHERE
//            knowclassify.knowObjectID =  '{$mygrade}' AND
//            knowclassify.knowSubjectID =  '{$mysubject}'";
//       $classify=KnowclassifyModel::findBySql($sql)->asArray()->one();
//        $classify= KnowclassifyModel::find()->where('knowObjectID = :objectID AND knowSubjectID=:subjectID',[':objectID'=>$mygrade,':subjectID'=>$mysubject])
//                ->asArray()->all();
//       $i=0;
//       $pager=new Pagination(['totalCount'=>count($classify),'pageSize'=>1]);
//       $data=self::find()->where('knowID=:knowID',[':knowID'=>$value['knowID']])
//               ->offset($pager->offset)->limit($pager->limit)->all();
//        foreach ($classify as $value){
//            $know[$i++]= self::find()->where('knowID=:knowID',[':knowID'=>$value['knowID']])->one();
//        }
         /*        $obj=$model->find();
        $count=$obj->count();
        $pager=new Pagination(['totalCount'=>$count,'pageSize'=>7]);
        $data=$obj->offset($pager->offset)->limit($pager->limit)->all();
         */
        //目前只是返回了数据，没有分页数组
        
    }
}
